/**
 * JAVACC DEMO 1.0
 */
package com.apache.portal.service.plugin;

import com.apache.api.vo.ResultMsg;
import com.apache.client.InfoFavoriteCoreClient;
import com.apache.client.InfoReleaseCoreCleint;
import com.apache.client.LogClient;
import com.apache.info.entity.PubMetadata;
import com.apache.portal.common.PortalPlugin;
import com.apache.portal.common.annotion.AnntationBean;
import com.apache.portal.common.enums.InfoMethodEnum;
import com.apache.tools.ConfigUtil;
import com.apache.tools.DateUtils;
import com.apache.tools.RequestTools;
import com.apache.tools.StrUtil;
import com.apache.uct.common.LoginUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * description:  消息通知插件
 *
 * @author Hou Dayu 创建时间：2016-8-29
 */
@AnntationBean(name = "favorite")
public class FavoriteSystemPluginImpl implements PortalPlugin {

    public Object doInvoke(HttpServletRequest request, HttpServletResponse response,
            Map<String, String> params) {
        Object result = null;
        String doCode = request.getParameter("doCode");
        if (StrUtil.isNull(doCode)) {
            doCode = params.get("doCode");
        }
        LoginUser loginUser = (LoginUser) request.getSession().getAttribute("loginUser");
        String sysPass = ConfigUtil.getInstance().interfacePass();
        params.put("sysPass", sysPass);
        switch (InfoMethodEnum.getName(doCode)) {
        case Fsave://收藏添加
            params.put("userEname", loginUser.getUserEname());
            result = saveInfo(request, params, "keep_info");
            break;
        case Fedit://收藏修改
            result = editInfo(request, params, "keep_info");
            break;
        case Fdel://收藏删除
            result = delInfo(request, params, "keep_info");
            break;
        case Msave://消息添加
            params.put("createdUser", loginUser.getUserEname());
            params.put("noticeStatus", "0");
            result = saveInfo(request, params, "notice");
            break;
        case Medit://消息修改
            result = editInfo(request, params, "notice");
            break;
        case Mdel://消息删除
            result = delInfo(request, params, "notice");
            break;
        case Fcount:
            params.put("userEname", loginUser.getUserEname());
            params.put("sortCode", "keep_info");
            long fcount = InfoFavoriteCoreClient.getInstance().countInfo(params);
            result = fcount;
            break;
        case Mcount:
            params.put("userEname", loginUser.getUserEname());
            params.put("sortCode", "notice");
            long mcount = InfoFavoriteCoreClient.getInstance().countInfo(params);
            result = mcount;
            break;
        }
        return result;
    }

    private ResultMsg saveInfo(HttpServletRequest request, Map<String, String> params,
            String sortCode) {
        ResultMsg json = null;
        params.put("sortCode", sortCode);
        List<PubMetadata> list = InfoReleaseCoreCleint.getInstance()
                .getMetadatas(sortCode, params.get("sysName"));
        if (null == list)
            return null;
        for (PubMetadata pm : list) {
            if ("1".equals(pm.getDataRestrict())) {//处理主键
                params.put(pm.getDataAttr(), UUID.randomUUID().toString().replaceAll("-", ""));
            } else if ("6".equals(pm.getDataType())) {//处理日期类型
                if (StrUtil.isNull(params.get(pm.getDataAttr()))) {
                    params.put(pm.getDataAttr(),
                            DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                }
            } else if ("2".equals(pm.getDataRestrict())) {
                if (StrUtil.isNull(params.get(pm.getDataAttr()))) {
                    json = new ResultMsg("F", "表单的必填项不可为空");
                    return json;
                }
            }
        }
        json = InfoFavoriteCoreClient.getInstance().saveInfo(params);
        if (null != json && "T".equals(json.getFlag())) {
            this.addLogInfo("save", "com.apache.pubInfo." + sortCode, request, params);
        }
        return json;
    }

    private ResultMsg editInfo(HttpServletRequest request, Map<String, String> params,
            String sortCode) {
        ResultMsg json = null;
        params.put("sortCode", sortCode);
        PubMetadata pm = InfoReleaseCoreCleint.getInstance()
                .getMetadataPrimarykey(sortCode, params.get("sysName"));
        if (null != pm) {
            if ("1".equals(pm.getDataRestrict())) {//处理主键
                if (StrUtil.isNull(params.get(pm.getDataAttr()))) {
                    json = new ResultMsg("F", "非空参数不可为空");
                    return json;
                }
            }
        }
        json = InfoFavoriteCoreClient.getInstance().updateInfo(params);
        if (null != json && "T".equals(json.getFlag())) {
            this.addLogInfo("edit", "com.apache.pubInfo." + sortCode, request, params);
        }
        return json;
    }

    private ResultMsg delInfo(HttpServletRequest request, Map<String, String> params,
            String sortCode) {
        ResultMsg json = null;
        params.put("sortCode", sortCode);
        PubMetadata pm = InfoReleaseCoreCleint.getInstance()
                .getMetadataPrimarykey(sortCode, params.get("sysName"));
        if (null != pm) {
            if ("1".equals(pm.getDataRestrict())) {//处理主键
                if (StrUtil.isNull(params.get(pm.getDataAttr()))) {
                    json = new ResultMsg("F", "非空参数不可为空");
                    return json;
                }
            }
        }
        json = InfoFavoriteCoreClient.getInstance().deleteInfo(params);
        if (null != json && "T".equals(json.getFlag())) {
            this.addLogInfo("del", "com.apache.pubInfo." + sortCode, request, params);
        }
        return json;
    }

    /**
     * description:  添加审记信息
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    private void addLogInfo(String logType, String className, HttpServletRequest request, Map map) {
        LoginUser loginUser = (LoginUser) request.getSession().getAttribute("loginUser");
        if (null != loginUser) {
            Map<String, Object> logMap = new HashMap<String, Object>();
            logMap.put("logType", logType);
            logMap.put("logClassName", className);
            logMap.put("logIp", RequestTools.getIp(request));
            logMap.put("log_userEname", loginUser.getUserEname());
            logMap.putAll(map);
            LogClient.getInstance().execute(logMap);//添加日志
        }
    }
}
